package handle

import (
	"context"
	"net/http"

	"protocx/protobufx"

	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/app/server"
)

func NewProtobuf() Protobuf {
	return Protobuf{}
}

type Protobuf struct {
}

func (r Protobuf) InitRoute(server *server.Hertz) {
	p := server.Group("/protobuf")
	p.GET("/", r.ReadProtobuf())
	p.POST("/", r.Protobuf())
}
func (r Protobuf) Protobuf() app.HandlerFunc {
	return func(c context.Context, ctx *app.RequestContext) {
		ctx.ProtoBuf(http.StatusOK, &protobufx.LoginRequest{
			Code: 879,
		})
	}
}
func (r Protobuf) ReadProtobuf() app.HandlerFunc {
	return func(c context.Context, ctx *app.RequestContext) {
		var request protobufx.LoginRequest
		err := ctx.BindProtobuf(&request)
		if err != nil {
			ctx.JSON(400, err)
			return
		}
		ctx.JSON(http.StatusOK, request)
	}
}
